package com.tzkyzj.tiremanager.repository;


import com.tzkyzj.tiremanager.entity.SettlementRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public interface SettlementRecordRepository extends JpaRepository<SettlementRecord, Long> {


    // 添加分页查询方法
    List<SettlementRecord> findAllByOrderBySettlementDateDesc(Pageable pageable);

    List<SettlementRecord> findByPlateNumberContainingIgnoreCase(String plateNumber);

    List<SettlementRecord> findByTransportCompany(String transportCompany);

    List<SettlementRecord> findBySettlementDateBetween(LocalDate startDate, LocalDate endDate);

    @Query("SELECT DISTINCT s.plateNumber FROM SettlementRecord s WHERE s.plateNumber LIKE %:keyword%")
    List<String> findDistinctPlateNumbers(@Param("keyword") String keyword);

    @Query("SELECT s FROM SettlementRecord s ORDER BY s.createTime DESC")
    List<SettlementRecord> findAllOrderByCreateTimeDesc();


    // 在 SettlementRecordRepository 接口中添加以下方法

    // 根据运输公司排序查询（分页）
    List<SettlementRecord> findByTransportCompanyOrderBySettlementDateDesc(String transportCompany, Pageable pageable);

    // 根据车牌号排序查询（分页）
    List<SettlementRecord> findByPlateNumberOrderBySettlementDateDesc(String plateNumber, Pageable pageable);

    // 根据运输公司和关键字查询车牌号
    @Query("SELECT DISTINCT s.plateNumber FROM SettlementRecord s WHERE s.plateNumber LIKE %:keyword% AND s.transportCompany = :company")
    List<String> findDistinctPlateNumbersByCompany(@Param("keyword") String keyword, @Param("company") String company);



    // 根据车牌号查询
    List<SettlementRecord> findByPlateNumber(String plateNumber);

    // 根据运输公司和日期范围查询
    List<SettlementRecord> findByTransportCompanyAndSettlementDateBetween(String transportCompany, LocalDate startDate, LocalDate endDate);

    // 根据车牌号和日期范围查询
    List<SettlementRecord> findByPlateNumberAndSettlementDateBetween(String plateNumber, LocalDate startDate, LocalDate endDate);

    // 查询所有运输公司
    @Query("SELECT DISTINCT s.transportCompany FROM SettlementRecord s WHERE s.transportCompany IS NOT NULL")
    List<String> findDistinctTransportCompanies();
}
